/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"
#include "../big_buf/head.h"
#include "../impl/head.h"
#include "../buf/head.h"
#include "msg_impl.h"

/////////////////////////////////////////////////////////////////////////////////////////
// 
namespace xos_box
{
    ////////////////////////////////////////////////////////////////////////////////////
    // 

    static msg_impl::ITEM_DATA xos_item_data;

    ////////////////////////////////////////////////////////////////////////////////////

    msg_impl::msg_impl()
    {
        memset( m_lpRun, 0, sizeof( m_lpRun ) );
        memset( m_lpBigBuf, 0, sizeof( m_lpBigBuf ) );
        memset( m_lpBuf, 0, sizeof( m_lpBuf ) );
        memset( m_lpData, 0, sizeof( m_lpData ) );
        memset( m_dlData, 0, sizeof( m_dlData ) );
        memset( m_nData, 0, sizeof( m_nData ) );
        memset( m_nMsg, 0, sizeof( m_nMsg ) );
        memset( m_i64, 0, sizeof( m_i64 ) );
        memset( m_bData, 0, sizeof( m_bData ) );
    }

    msg_impl::~msg_impl()
    {
        term();
    }

    ////////////////////////////////////////////////////////////////////////////////////
    // 

    int msg_impl::get_xos_item_data( ITEM_DATA*& item_ptr )
    {
        int ret = 0;
        xos_item_data.set_container( mgr::container() );
        item_ptr = &xos_item_data;
        return ret;
    }

    int msg_impl::init()
    {
        int nRet = 0;
        return nRet;
    }

    int msg_impl::term()
    {
        int nRet = 0;
        return nRet;
    }
    //////////////////////////////////////////////////////////////////////////////////////
    // 
    // data member
    //

    // 
    // string
    // 
    const char * msg_impl::get_str( int nIndex, char * lpszStr, int nSize, int * lpnLenNeeded )
    {
        nIndex = normalize_index( nIndex );
        const char * lpRet = m_strs[nIndex].c_str();

        if( lpnLenNeeded )
        {
            *lpnLenNeeded = ( int )strlen( lpRet );
        }

        if( lpszStr )
        {
            mgr::container()->crt()->strcpy( lpszStr, nSize, lpRet );
        }

        return lpRet;
    }

    int msg_impl::set_str( int nIndex, const char * lpszStr )
    {
        int nRet = 0;

        nIndex = normalize_index( nIndex );
        if( lpszStr )
        {
			m_strs[nIndex] = lpszStr;
        }
        else
        {
			m_strs[nIndex] = "";
        }

        return nRet;
    }

    // 
    // xos_box::i_big_buf
    // 
    xos_box::i_big_buf * msg_impl::get_big_buf( int nIndex, xos_box::i_big_buf ** lppBigBuf )
    {
        nIndex = normalize_index( nIndex );
        xos_box::i_big_buf * lpBuf = m_lpBigBuf[nIndex];

        if( lppBigBuf )
        {
            *lppBigBuf = lpBuf;
        }

        return lpBuf;
    }

    int msg_impl::set_big_buf( int nIndex, xos_box::i_big_buf * lpBigBuf )
    {
        int nRet = 0;
        nIndex = normalize_index( nIndex );
        m_lpBigBuf[nIndex] = ( big_buf_impl * )lpBigBuf;
        return nRet;
    }

    // 
    // xos_box::i_buf
    // 
    xos_box::i_buf * msg_impl::get_buf( int nIndex, xos_box::i_buf ** lppBuf )
    {
        nIndex = normalize_index( nIndex );
        xos_box::i_buf * lpBuf = m_lpBuf[nIndex];

        if( lppBuf )
        {
            *lppBuf = lpBuf;
        }

        return lpBuf;
    }

    int msg_impl::set_buf( int nIndex, xos_box::i_buf * lpBuf )
    {
        int nRet = 0;
        nIndex = normalize_index( nIndex );
        m_lpBuf[nIndex] = ( buf_impl * )lpBuf;
        return nRet;
    }

    // 
    // double
    // 
    double msg_impl::get_double( int nIndex, double * lpdlData )
    {
        nIndex = normalize_index( nIndex );
        if( lpdlData )
        {
            *lpdlData = m_dlData[nIndex];
        }

        return m_dlData[nIndex];
    }

    int msg_impl::set_double( int nIndex, double dlData )
    {
        int nRet = 0;
        nIndex = normalize_index( nIndex );
        m_dlData[nIndex] = dlData;
        return nRet;
    }

    // 
    // void *
    // 
    void * msg_impl::get_void( int nIndex, void ** lppData )
    {
        nIndex = normalize_index( nIndex );
        void * pRet = m_lpData[nIndex];

        if( lppData )
        {
            *lppData = pRet;
        }

        return pRet;
    }

    int msg_impl::set_void( int nIndex, void * lpData )
    {
        int nRet = 0;
        nIndex = normalize_index( nIndex );
        m_lpData[nIndex] = lpData;
        return nRet;
    }

    // 
    // int
    // 
    int msg_impl::get_int( int nIndex, int * lpnInt )
    {
        nIndex = normalize_index( nIndex );
        int nRet = m_nData[nIndex];

        if( lpnInt )
        {
            *lpnInt = nRet;
        }

        return nRet;
    }

    int msg_impl::set_int( int nIndex, int nInt )
    {
        int nRet = 0;
        nIndex = normalize_index( nIndex );
        m_nData[nIndex] = nInt;
        return nRet;
    }

    // 
    // int64_t
    // 
    int64_t msg_impl::get_i64( int nIndex, int64_t * lpi64 )
    {
        nIndex = normalize_index( nIndex );
        if( lpi64 )
        {
            *lpi64 = m_i64[nIndex];
        }

        return m_i64[nIndex];
    }

    int msg_impl::set_i64( int nIndex, int64_t i64 )
    {
        nIndex = normalize_index( nIndex );
        m_i64[nIndex] = i64;
        return 0;
    }

    // 
    // bool
    // 
    bool msg_impl::get_bool( int nIndex, bool * lpBool )
    {
        nIndex = normalize_index( nIndex );
        if( lpBool )
        {
            *lpBool = m_bData[nIndex];
        }

        return m_bData[nIndex];
    }

    int msg_impl::set_bool( int nIndex, bool bBool )
    {
        nIndex = normalize_index( nIndex );
        m_bData[nIndex] = bBool;
        return 0;
    }

    // 
    // xos::f_runnable
    // 
    xos::f_runnable msg_impl::get_runnable( int nIndex, xos::f_runnable * lpRun )
    {
        nIndex = normalize_index( nIndex );
        xos::f_runnable lpBuf = m_lpRun[nIndex];

        if( lpRun )
        {
            *lpRun = lpBuf;
        }

        return lpBuf;
    }

    int msg_impl::set_runnable( int nIndex, xos::f_runnable lpRun )
    {
        int nRet = 0;
        nIndex = normalize_index( nIndex );
        m_lpRun[nIndex] = lpRun;
        return nRet;
    }

    // 
    // msg
    // 
    int msg_impl::get_msg( int nIndex, int* lpnInt )
    {
        nIndex = normalize_index( nIndex );
        int nRet = m_nMsg[nIndex];

        if( lpnInt )
        {
            *lpnInt = nRet;
        }

        return nRet;
    }

    int msg_impl::set_msg( int nIndex, int nInt )
    {
        int ret = 0;
        nIndex = normalize_index( nIndex );
        m_nMsg[nIndex] = nInt;
        return ret;
    }

    //////////////////////////////////////////////////////////////////////////////////////
    // 

    // 
    // 因为太经常用了，所以加两对方法支持一下
    // 
    int msg_impl::set_msg_type( int nType )
    {
        int ret = 0;
        set_msg( -1, nType );
        return ret;
    }

    int msg_impl::set_msg( int nMsg )
    {
        int ret = 0;
        set_msg( 0, nMsg );
        return ret;
    }

    int msg_impl::get_msg_type()
    {
        int ret = 0;
        ret = get_msg( -1, 0 );
        return ret;
    }

    int msg_impl::get_msg()
    {
        int ret = 0;
        ret = get_msg( 0, 0 );
        return ret;
    }

    //////////////////////////////////////////////////////////////////////////////////////
    // 

    int msg_impl::normalize_index( int nIndex )
    {
        int ret = nIndex;
        if( ret < 0 )
        {
            ret = EXTRA_PARAM_NUM + ret;
        }
        return ret;
    }

} // xos
